/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package formatconversion_model;

import magic_model.Card;
import magic_model.Deck;
import magic_model.MalformedFileException;


/**
 *
 * @author Tobias
 */
public class MWS_Parser implements MagicFormatParser{

    public Deck importFile(String[] lines) {
        //ArrayList<String> tempLines = new ArrayList<String>();
        Deck deck = new Deck();
        for(int i=0;i<lines.length;i++){
            parseLine(lines[i],deck);
        }
        deck.sort();
        return deck;
    }
    
    private void parseLine(String line,Deck deck) {
     // System.out.println("parsing: "+line);
        line = line.trim();
      
        if (line.equals(""))return;
        
        if(line.contains("//")){
           if(line.contains("NAME")){
               String[] tmp= line.split(":");
               if(tmp.length==2){
               String name= tmp[1].trim();
               deck.setName(name);
               }
            }
           return;
        }
        
          boolean isMain=true;
        if(line.contains("SB:")){
          line = line.replace("SB:","");
          isMain=false;
        }
        int bracketStart = line.indexOf("["); 
        int bracketEnd   = line.indexOf("]");
            
            
        if(bracketStart==-1 && bracketEnd==-1){
            //no brackets at all => .dec format
            String [] temp = line.split(" ");
            int quantity = Integer.parseInt(temp[0]);
            String name = temp[1];
            deck.addCard(new Card(quantity,name,isMain));
            //throw new MalformedFileException("File is not in MWS-Format: "+line);
        }else{
           if(bracketStart!=-1 && bracketEnd!=-1){
               //.mwDeck Format
                String quantityS=line.substring(0, bracketStart).trim();
                String name =line.substring(bracketEnd+1).trim();
                if(name.contains("(")&&name.contains(")"))
                    name=name.substring(0,name.indexOf("("));
                int quantity = Integer.parseInt(quantityS);
                deck.addCard(new Card(quantity, name, isMain));
           }else{
                 
            throw new MalformedFileException("File has an odd number of brackets. "+line);
           }
           
        }
      

       
    }

    public String[] exportFile(Deck intern) {
        intern.sort();
        String[] content = new String[intern.getCards().size()+1];
        content[0] = "// NAME : "+intern.getName();
        int i=1;
        for(Card c: intern.getCards()){
             String line="";
            if(!c.isMain()){
                line="SB:\t";
            }
           line+=c.getQuantity()+" "+c.getName();
            content[i]=line;
            i++;
        }
        return content;
    }
    
  @Override
    public String toString(){
        return "Magic Workstation Format";
    }

    public FileType getFileType() {
        return FileType.mws;
    }
    
    
}
